package com.dj.mingkong.common.microservice;

import com.dj.mingkong.common.infrastructure.base.dto.context.UserInfoContext;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public enum UserContext {
    ;

    private UserContext() {
    }

    public static Object getUserObject() {
        return ServiceContext.getContext().get("user_code");
    }

    public static UserInfoContext getUserInfoContext() {
        return (UserInfoContext) ServiceContext.getContext().get("user_code");
    }

    public static Long getUserId() {
        return (Long) ServiceContext.getContext().get("user_id");
    }

    public static String getUsername() {
        return (String) ServiceContext.getContext().get("username");
    }

    public static List<Object> getUserRole() {
        return (List) ServiceContext.getContext().get("user_role");
    }

    public static void setUserObject(Object userObject) {
        ServiceContext.getContext().set("user_code", userObject);
    }

    public static void removeUserObject() {
        ServiceContext.getContext().remove("user_code");
    }

    public static void removeUserId() {
        ServiceContext.getContext().remove("user_id");
    }

    public static void setUserId(Long id) {
        ServiceContext.getContext().set("user_id", id);
    }

    public static void setUsername(String username) {
        ServiceContext.getContext().set("username", username);
    }

    public static void setUserRole(List<Object> roles) {
        ServiceContext.getContext().set("user_role", roles);
    }

    public static void addUserRole(Object role) {
        if (CollectionUtils.isEmpty(getUserRole())) {
            List<Object> roles = new ArrayList();
            roles.add(role);
            setUserRole(roles);
        } else {
            getUserRole().add(role);
        }

    }
}
